package com.mlj.service;

import com.mlj.dao.ClassesMapper;
import com.mlj.pojo.Admin;
import com.mlj.pojo.Classes;
import com.mlj.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class ClassesServiceImpl implements ClassesService{
    @Autowired
    ClassesMapper classesMapper;
    @Override
    public List<Classes> getClassName() {
        return classesMapper.selectAll();
    }

    @Override
    public Result getClassList(Integer page, Integer limit) {
        //计算分页查询的起始位置
        Integer start = (page-1)*limit;
        Result result = new Result();
        //分页查询列表
        List<Classes> classes = classesMapper.findClassListByPage(start,limit);
        result.setItem(classes);//获取数据
        Integer count = classesMapper.selectCount();
        result.setTotal(count);//数据长度
        return result;
    }

    @Override
    public Result delClassById(Integer id) {
        Result result = new Result();
        try{
            classesMapper.delClassById(id);
            result.setStatus(200);//自己编的响应码 200就代表成功
            result.setMessage("删除成功");
        }catch (Exception e){
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            result.setStatus(500);//自己编的响应码，代表错误
            result.setMessage("网络延迟，删除失败");
        }
        return result;
    }

    @Override
    public Result deleteBatch(Integer[] ids) {
        Result result = new Result();
        try{
            classesMapper.deleteBatch(ids);
            result.setStatus(200);//自己编的响应码 200就代表成功
            result.setMessage("删除成功");
        }catch (Exception e){
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            result.setStatus(500);//自己编的响应码，代表错误
            result.setMessage("网络延迟，删除失败");
        }
        return result;
    }

    @Override
    public Result addClass(Classes classes) {
        Result result = new Result();
        try{
            classesMapper.insertClass(classes);
            result.setStatus(200);//自己编的响应码 200就代表成功
            result.setMessage("添加成功");
        }catch (Exception e){
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            result.setStatus(500);//自己编的响应码，代表错误
            result.setMessage("网络延迟，添加失败");
        }
        return result;
    }

    @Override
    public Result updateClass(Classes classes) {
        Result result = new Result();
        try{
            classesMapper.updateByPrimaryKey(classes);
            result.setStatus(200);//自己编的响应码 200就代表成功
            result.setMessage("修改成功");
        }catch (Exception e){
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            result.setStatus(500);//自己编的响应码，代表错误
            result.setMessage("网络延迟，修改失败");
        }
        return result;
    }

    @Override
    public Result search(String profession, String department, Integer page, Integer limit) {
        Result result = new Result();
        //使用map存储传递参数
        Map<String,Object> paraMaps = new HashMap<>();
        //计算分页查询起始位置
        Integer start = (page-1)*limit;
        paraMaps.put("start",start);
        paraMaps.put("limit",limit);
        paraMaps.put("department",department);
        paraMaps.put("profession",profession);
        //根据时间范围和关键字进行分页查询
        List<Admin> searchAdmins = classesMapper.searchClassByKw(paraMaps);
        result.setItem(searchAdmins);
        //搜索视频的长度,便于搜索之后分页
        Integer count = classesMapper.searchCountByKw(paraMaps);
        result.setTotal(count);
        return result;
    }
}
